<?php

	if(!defined('YMT')) exit;

	/**
	 * @package Yomm
	 * @author bsuter
	 * 
	 * Performe a MySQL select query. The Yomm_Db_Plugin_Select
	 * class is only to make the old ydsql class functions available.
	 * The new Select class will be Yomm_Db_Plugin_Query.
	 */
	Class Yomm_Db_Plugin_Select Extends Yomm_Db_Plugins {
		
		private $data = null;
		
		/* select($sql_table,$sql_fields,$sql_options=false,$sql_return_as_array=false,$sql_fetch_mode='object') */
		public function __construct($table, $fields, $options = null, $mutlirow_return = true, $fetch_mode = 'object')
		{
			if(is_array($fields))
			{
				//$fields = array();
				$tmp_fields = null;
				$i = 0;
				// $fields = implode(",", $fields);
				foreach($fields as $key => $value)
				{
					$i++;
					if(is_int($key)){
						$tmp_fields .= self::$mysqli->real_escape_string($value);
					}else{
						$tmp_fields .= $key.' as '. self::$mysqli->real_escape_string($value);
					}
					
					if(count($fields)!=$i) $tmp_fields .= ',';
				}
				
				$fields = $tmp_fields;
			}
			
			$query = 'SELECT '.$fields.' FROM `'.$table.'` '.$options;
			
			
			$result = self::$mysqli->query($query);
			
			$data = array();
		
			if($result === false)
			{
				throw new Yomm_Error("MySQLi Error: \"".self::$mysqli->error."\" for query: " . $query);
				return;
			}
			
			while ($row = $result->fetch_object())
			{
				if($fetch_mode == 'array')
				{
					$data[] = Yomm_Library_Convert::objectToArray($row);
				} else {
					$data[] = $row;
				}
			}
			
			$result->close();
			
			if($mutlirow_return === false)
			{
				if(count($data) == 0)
				{
					$data = array();
				} else {
				
					$data = $data[0];
				}
			}
			
			$this->data = $data;
			
		}
		
		public function returnValue()
		{
			
			return $this->data;
		}
		
	}